﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityMultiPlayerGameServer.Data;
using GameProtocol;

namespace UnityMultiPlayerGameServer.Manager
{
    internal class RoomManager
    {
        private static readonly object locker = new object();
        private static RoomManager instance;
        public static RoomManager Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null)
                        {
                            instance = new RoomManager();
                        }
                    }
                }
                return instance;
            }
        }

        /// <summary>
        /// 存放所有房间的字典
        /// key -> 房间名 
        /// </summary>
        private Dictionary<string, Room> roomDict = new Dictionary<string, Room>();


        public void AddRoom(Room room)
        {
            roomDict.Add(room.RoomInfo.RoomName, room);
        }

        public void RemoveRoom(Room room)
        {
            roomDict.Remove(room.RoomInfo.RoomName);
        }

        public Dictionary<string, Room> GetRoomDict()
        {
            return roomDict;
        }

        public bool isRoomExist(string roomName)
        {
            return roomDict.ContainsKey(roomName);
        }

        public Room TryGetRoomByRoomname(string roomname)
        {
            if(isRoomExist(roomname))
            {
                return roomDict[roomname];
            }
            return null;
        }
    }
}
